package com.hanxiaozhang.autocinfigure;

import com.hanxiaozhang.DistributedLockFactory;
import com.hanxiaozhang.DistributedLockFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 〈一句话功能简述〉<br>
 * 〈分布式锁自动注入配置〉
 *
 * @author hanxinghua
 * @create 2022/9/3
 * @since 1.0.0
 */
@Configuration
@EnableConfigurationProperties(value = {DistributedLockProperties.class})
// 配置文件中enabled是true时，启动自动注入
@ConditionalOnProperty(prefix = "distributed-lock", value = "enabled", havingValue = "true")
public class DistributedLockAutoConfigure {

    @Autowired
    private DistributedLockProperties properties;

    @Bean
    @ConditionalOnMissingBean
    DistributedLockFactory distributedLockFactory() {
        return new DistributedLockFactoryBuilder()
                .build(properties);
    }

}
